//
//  LJFSearchResultController.m
//  SevenZhou
//
//  Created by qianfeng on 15/9/18.
//  Copyright (c) 2015年 ljf. All rights reserved.
//

#import "LJFSearchResultController.h"
#import "LJFSearchResultCell.h"
#import "Connector.h"
#import "LJFHttpManager.h"
#import "LJFjinDianModel.h"
#import "UIImageView+AFNetworking.h"
#import "LJFCustomController.h"
#import "MJRefresh.h"
#import "LJFDetailController.h"
#import "LJFHomeController.h"
#import "LJFselectModel.h"
#import "LJFSortView.h"
#import "LJFSubjects.h"
#import "LJFContinentModel.h"
#import "MBProgressHUD.h"


@interface LJFSearchResultController ()<UITableViewDataSource,UITableViewDelegate,MJRefreshBaseViewDelegate,DropDownChooseDelegate,DropDownChooseDataSource,SortViewremoveDelegate,drowDownRemoveDelegate>
{
    NSMutableArray *_dataArr;
    
    MJRefreshFooterView *_footView;
    MJRefreshHeaderView *_headView;
    
    BOOL _isDrawDown;
    
    NSMutableArray *chooseArray ;
    
    NSArray *_CategoryArr;
    NSArray *_sortArr;
    
    BOOL _isCondition;
//    BOOL _isConditionRefresh;
    
    NSMutableArray *_sevenArr;
    
    LJFSortView *_Sortview;
    
    MBProgressHUD *_HUD;
}

@property (weak, nonatomic) IBOutlet UITableView *myTableView;

@end

@implementation LJFSearchResultController

#pragma mark - 初始化

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self)
    {
        _dataArr = [[NSMutableArray alloc] init];
        _sevenArr = [[NSMutableArray alloc] init];
        
        [self variableInit];
    }
    return self;
}

#pragma mark - 初始化加载参数

- (void)variableInit
{
    _page = 1;
    _brand_id = @"";
    _category_name = @"";
    _category_id = @"";
    _keywords = @"";
    _dest_id = @"";
    _sort_by =@"";
}

#pragma mark -加载View

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self hiddenTabar];
    
    [self loadDestDataFromNet];
    
    [self loadDataFromNet];
    
    [self createRefresh];
    
    //[self createDrawDownView];
}

#pragma mark - tabar显示隐藏
- (void)hiddenTabar
{
    LJFCustomController *tabar = (LJFCustomController *)self.tabBarController;
    
    tabar.tabBarView.hidden = YES;
}
- (void)showTabar
{
    LJFCustomController *tabar = (LJFCustomController *)self.tabBarController;
    
    tabar.tabBarView.hidden = NO;
}

#pragma mark - 加载网络数据

//  加载七大洲数据
- (void)loadDestDataFromNet
{
    LJFHttpManager *manager = [LJFHttpManager shareInstance];
    
    [manager getDataFromNetWithUrl:URL_SevenZhouData AndParameterStr:@"" success:^(id responseObj)
     {
         NSDictionary *dict = responseObj;
         
         NSMutableArray *arrM = [[NSMutableArray alloc] init];
         
         for (NSDictionary *dic in dict[@"data"])
         {
             LJFContinentModel *model = [[LJFContinentModel alloc] initWithDictionary:dic];
             
             [arrM addObject:model];
         }
         
         _sevenArr = [NSMutableArray arrayWithArray:arrM];
         
         [self loadCategoryDataFromNet];
         
     } failure:^(NSError *error)
     {
         
         NSLog(@"error == %@",error);
         
     }];
}

//加载分类数据

- (void)loadCategoryDataFromNet
{
    LJFHttpManager *manager = [LJFHttpManager shareInstance];
    
    [manager getDataFromNetWithUrl:URL_Category AndParameterStr:URL_Category_parameter success:^(id responseObj)
     {
         
         NSDictionary *dict = responseObj;
         
         NSMutableArray *arrM = [[NSMutableArray alloc] init];
         
         LJFselectModel *model = [[LJFselectModel alloc] init];
         
         model.name = @"全部分类";
         model.myid = @"0";
         model.isSelect = YES;
         
         [arrM addObject:model];
         
         for (NSDictionary *dic in dict[@"data"])
         {
             
             LJFselectModel *model = [[LJFselectModel alloc] init];
             
             model.name = dic[@"name"];
             model.myid = dic[@"id"];
             model.isSelect = NO;
             
             [arrM addObject:model];
             
         }
         _CategoryArr = [NSArray arrayWithArray:arrM];
         
         [self createDrawDownView];
         [self creatSortview];
         
     } failure:^(NSError *error)
     {
         NSLog(@"error == %@",error);
     }];
}

- (void)showProgress
{
    _HUD = [[MBProgressHUD alloc] initWithView:self.view];
    _HUD.labelText = @"正在努力加载中";
    
    [self.view addSubview:_HUD];
    
    [_HUD show:YES];
    
}

//加载搜索数据
- (void)loadDataFromNet
{
    [self showProgress];
    
    NSString *parameter = [NSString stringWithFormat:URL_Search_parameter,_page,_brand_id,_category_id,_keywords,_category_name,_dest_id,_sort_by];
    
    LJFHttpManager *manager = [LJFHttpManager shareInstance];
    
    [manager getDataFromNetWithUrl:URL_Search AndParameterStr:parameter success:^(id responseObj)
     {
         //上拉刷新 删除数据源数据
         if (_isDrawDown)
         {
             [_dataArr removeAllObjects];
             _isDrawDown = NO;
         }
         
         NSDictionary *dict = responseObj;
         
         for (NSDictionary *dic in dict[@"data"])
         {
             LJFjinDianModel *model = [[LJFjinDianModel alloc] initWithDictionary:dic];
             
             [_dataArr addObject:model];
         }
         
         NSNumber *more = dict[@"paginated"][@"more"];
         
         self.isMore = [more boolValue];
         
         [_footView endRefreshing];
         [_headView endRefreshing];
         
         [_myTableView reloadData];
         
         [_HUD hide:YES];
         
     } failure:^(NSError *error)
     {
         NSLog(@"error == %@",error);
         
     }];
}

//按条件搜索加载数据

- (void)loadDataFromNetWithCondition
{
    [self showProgress];
    
    if (_category_id.length == 0)
    {
        _category_id = @"0";
    }
    
    if (_dest_id.length == 0)
    {
        _dest_id = @"0";
    }
    
    NSString *parameter = [NSString stringWithFormat:URL_Search_parameter1,_page,_brand_id,_category_id,_category_name,_dest_id,_sort_by];
    
    LJFHttpManager *manager = [LJFHttpManager shareInstance];
    
    [manager getDataFromNetWithUrl:URL_Search AndParameterStr:parameter success:^(id responseObj)
     {
         if (_isDrawDown)
         {
             [_dataArr removeAllObjects];
             _isDrawDown = NO;
         }
         
         NSDictionary *dict = responseObj;
         
         for (NSDictionary *dic in dict[@"data"])
         {
             LJFjinDianModel *model = [[LJFjinDianModel alloc] initWithDictionary:dic];
             
             [_dataArr addObject:model];
         }
         
         NSNumber *more = dict[@"paginated"][@"more"];
         
         self.isMore = [more boolValue];
         
         [_footView endRefreshing];
         [_headView endRefreshing];
         
         [_myTableView reloadData];
         
         [_HUD hide:YES];
         
     } failure:^(NSError *error)
     {
         NSLog(@"error == %@",error);
         
     }];
}

#pragma mark - 初始化上拉下拉刷新

- (void)createRefresh
{
    _headView = [[MJRefreshHeaderView alloc] init];
    _headView.delegate = self;
    _headView.scrollView = _myTableView;
    
    _footView = [[MJRefreshFooterView alloc] init];
    _footView.delegate = self;
    _footView.scrollView = _myTableView;
    
}

#pragma mark - 控制器销毁 释放上拉下拉刷新
- (void)dealloc
{
    [_headView free];
    [_footView free];
    
}

#pragma mark - 判断是否加载更多
-(void)setIsMore:(BOOL)isMore
{
    _isMore = isMore;
    
    if (_isMore)
    {
        _footView.scrollView = _myTableView;
    }
    else
    {
        _footView.scrollView = nil;
    }
}

#pragma mark -MJRefreshBaseViewDelegate

-(void)refreshViewBeginRefreshing:(MJRefreshBaseView *)refreshView
{
    if (refreshView == _headView)
    {
        _isDrawDown = YES;
        _page = 1;
    }
    else
    {
        _isDrawDown = NO;
        _page++;
    }
    
    if (_isCondition)
    {
        [self loadDataFromNetWithCondition];
    }
    else
    {
        [self loadDataFromNet];
    }
    
}

#pragma mark - 创建选择条件按钮

#pragma mark - 创建drawDownView

- (void)createDrawDownView
{
    [self loadSortArr];
    
    chooseArray = [NSMutableArray arrayWithArray:@[_CategoryArr,_sortArr]];
    
    _dropDownView = [[DropDownListView alloc] initWithFrame:CGRectMake(0,64, self.view.frame.size.width, 40) dataSource:self delegate:self];
    _dropDownView.mydelegate = self;
    
    _dropDownView.mSuperView = self.view;
    [self.view addSubview:_dropDownView];
    
}

#pragma mark - 加载排行数组
- (void)loadSortArr
{
    NSArray *name = @[@"默认",@"销量",@"人气",@"从低到高",@"从高到低"];
    NSArray *myid = @[@"",@"top_sale",@"is_hot",@"price_asc",@"price_desc"];
    
    NSMutableArray *arrM = [[NSMutableArray alloc] init];
    
    for (int i = 0; i<5; i++)
    {
        LJFselectModel *model = [[LJFselectModel alloc] init];
        
        model.name = name[i];
        model.myid = myid[i];
        
        if (i == 0)
        {
            model.isSelect = YES;
        }
        else
        {
            model.isSelect = NO;
        }
        
        [arrM addObject:model];
    }
    
    _sortArr = [NSArray arrayWithArray:arrM];
}

#pragma mark -- dropDownListDelegate

-(void) chooseAtSection:(NSInteger)section index:(NSInteger)index
{
    _isCondition = YES;
//    _isConditionRefresh = YES;
    
    _page = 1;
    
    LJFselectModel *model = chooseArray[section][index];
    
    if (section == 0)
    {
        _category_id = model.myid;
    }
    else
    {
        _sort_by = model.myid;
    }
    
    _isDrawDown = YES;
    
    [self loadDataFromNetWithCondition];
}

#pragma mark -- dropdownList DataSource
-(NSInteger)numberOfSections
{
    return [chooseArray count];
}
-(NSInteger)numberOfRowsInSection:(NSInteger)section
{
    NSArray *arry =chooseArray[section];
    return [arry count];
}
-(NSString *)titleInSection:(NSInteger)section index:(NSInteger) index
{
    LJFselectModel *model = chooseArray[section][index];
    
    return model.name;
}
-(NSInteger)defaultShowSection:(NSInteger)section
{
    if (section == 0)
    {
        return _cateoryIndex;
    }
    else
    {
        return 1;
    }
}

#pragma mark -drowDownRemoveDelegate

//隐藏其它按钮数据
- (void)drowDownRemove
{
    _Sortview.isRemove = YES;
}

#pragma mark - 创建目的地button

- (void)creatSortview
{
    _Sortview= [[LJFSortView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width/3.0, 40)];
    _Sortview.supview = self.view;
    
    _Sortview.sevenArr = _sevenArr;
    
    _Sortview.mydelegate = self;
    
    if (_destName.length > 0)
    {
        _Sortview.buttonName = _destName;
    }
    
    [self.view addSubview:_Sortview];
}

#pragma mark -SortViewremoveDelegate

//隐藏其它按钮数据
- (void)SortViewremove
{
    _dropDownView.isRemove = YES;
}


//获得搜索条件,重新加载数据
- (void)getSelectedGoodsId:(NSString *)goodsid
{
    _isCondition = YES;
//    _isConditionRefresh = YES;
    
    _page = 1;
    
    _dest_id = goodsid;
    
    _isDrawDown = YES;
    [self loadDataFromNetWithCondition];
}


#pragma mark - 重写set方法

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


#pragma mark -UITableViewDataSource,UITableViewDelegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LJFjinDianModel *model = _dataArr[indexPath.row];
    
    LJFSearchResultCell *cell=[tableView dequeueReusableCellWithIdentifier:@"LJFSearchResultCell"];
    cell.model = model;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    LJFjinDianModel *model = _dataArr[indexPath.row];
    
    LJFDetailController *detail = [self.storyboard instantiateViewControllerWithIdentifier:@"LJFDetailController"];
    detail.goodsid = model.goods_id;
    
    [self.navigationController pushViewController:detail animated:YES];

}

#pragma mark - 返回上一个控制器

- (IBAction)back:(id)sender
{
    
    [self.navigationController popViewControllerAnimated:YES];
    
    //显示tabar
    [self showTabar];
}



@end
